package com.middlegroup.subjectpay.web.api;

import com.middlegroup.common.enums.ServiceType;
import com.middlegroup.common.model.ResponseEntity;
import com.middlegroup.common.model.TransResult;
import com.middlegroup.subjectpay.bind.SessionSubjectCode;
import com.middlegroup.subjectpay.dto.TransRequest;
import com.middlegroup.subjectpay.service.DeductService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 *
 * @author pc
 * @date 2019/1/15
 */
@RestController
public class DeductController extends BaseController {
    @Resource
    private DeductService deductService;

    /**通用代扣接口
     curl -i 'http://10.0.0.109:8089/deduct' -d 'name=成娓娓&phone=18321788695&cardNo=6217001210084599560&idCard=421302199202081224&amount=0.01&serviceType=DF&subjectCode=S000001&channelCode=CH0000003&merchantBillNo=merchantBillNo201901230001&billNo=billNo201901230001'
     * @param transRequest 交易参数
     * @return 通用json返回
     */
    @RequestMapping(value = "deduct")
    public ResponseEntity deduct(@Valid TransRequest transRequest, @SessionSubjectCode String subjectCode){
//    public ResponseEntity deduct(@Valid TransRequest transRequest){
        ResponseEntity ret = new ResponseEntity(ResponseEntity.STATUS_OK);
        transRequest.setServiceType(ServiceType.DEDUCT.name());
        //初始化交易结果
        TransResult transResult = new TransResult();
        transResult.setMerchantBillNo(transRequest.getMerchantBillNo());
        transResult.setBillNo(transRequest.getBillNo());
        transResult.setAmount(transRequest.getAmount());
        transResult.setTransStatus(0);
        transResult.setMsg("代扣处理中");
        transResult.setTransTime(System.currentTimeMillis());
        ret.setData(transResult);

        //调用通用代扣接口
        transResult = deductService.deduct(transRequest);

        if (null != transResult){
            return new ResponseEntity(ResponseEntity.STATUS_OK,transResult);
        }

        //如果通用代扣接口异常，则默认代扣处理中，防止重复交易
        transResult.setTransStatus(0);
        transResult.setMsg("代扣服务异常!请联系管理员核对状态!");
        transResult.setTransTime(System.currentTimeMillis());
        return ret;
    }
}
